<?php
	$_userid = $_userid == 1 && $userid  ? $userid : $_userid; // 管理员编辑其他会员信息权限
	
	if(!isset($action)) $action = 'manage';
	require_once 'form.class.php';
	require_once 'includes/yellowpage.class.php';
	$yellowpage = new yellowpage();
	$yellowpage->set_model('buy');
	$yellowpage->set_userid($_userid);
	foreach($MODEL AS $modelid=>$value)
	{
		if($value['modeltype']==9 && $value['tablename'] == 'buy') break;
	}
	$yellowpage->modelid = $modelid;

	switch($action){
		case 'add':
		// 关于PHP表单提交错误后返回内容消失问题
			header("Cache-control: private");
			
		if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
	 
		if($dosubmit)
		{
			$secondcatid = intval($secondcatid);
			$thirdcatid = intval($thirdcatid);
			if($thirdcatid)
			{
				$info['catid'] = $thirdcatid;
			}
			elseif($secondcatid)
			{
				$info['catid'] = $secondcatid;
			}
			else
			{
				$info['catid'] = $catid;
			}
			if(!$info['catid']) showmessage('栏目参数错误');
			/*if(in_array($_groupid,$M['add_check']))
			{
				$info['status'] = 99;
			}
			else
			{
				$info['status'] = 1;
			}*/
			$info['status'] = 99;
			require_once MOD_ROOT.'attachment/attachment.class.php';
			$attachment = new attachment($module, $info['catid']);
			$contentid = $yellowpage->add($info) ;
			
			// by aming 
			///$url = "?module=$module&action=web&userid=$_userid&category=buy&id=$contentid";
			//$db->query("UPDATE `".DB_PRE."yellowpage_buy` SET `url`='$url' WHERE `id`=$contentid");
			showmessage('发布成功！', url("?module=$module&file=$file&action=$action&userid=$_userid"));
	
		}
		else
		{
			if($M['ischeck'] && $company_user_infos['status'] == 0) showmessage('您的公司正在审核当中...','goback');
			if($company_user_infos['endtime'] && $company_user_infos['endtime']<TIME) showmessage('您的服务截至日期已到，请续费...','goback');
			foreach($MODEL AS $modelid=>$value)
			{
				if($value['modeltype']==9 && $value['tablename'] == 'buy') break;
			}
	
			require CACHE_MODEL_PATH.'yp_form.class.php';
			$content_form = new content_form($modelid);
			$data['catid'] = $catid;
			$forminfos = $content_form->get($data);
			include template('buy_add.html','yellowpage');
		}
		break;
		
	case 'manage':
		if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
	 
		$where = "status=99";
		$infos = $yellowpage->listinfo($where, '`id` DESC', $page, 30, 1);
		$pages = $yellowpage->pages;
		include template('buy_manage.html','yellowpage');
	break;

	case 'edit':
		if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
	 
		$id = intval($id);
		if($dosubmit)
		{
			$secondcatid = intval($secondcatid);
			$thirdcatid = intval($thirdcatid);
			if($thirdcatid)
			{
				$info['catid'] = $thirdcatid;
			}
			elseif($secondcatid)
			{
				$info['catid'] = $secondcatid;
			}
			else
			{
				$info['catid'] = $catid;
			}
			require_once MOD_ROOT.'attachment/attachment.class.php';
			$attachment = new attachment($module, $catid);
			$yellowpage->edit($id, $info);
			
			// by aming 
			//$url = "?module=$module&action=web&userid=$_userid&category=buy&id=$id";
			//$db->query("UPDATE `".DB_PRE."yellowpage_buy` SET `url`='$url' WHERE `id`=$id");
			showmessage('修改成功！',  $forward);
		}
		else
		{
			require CACHE_MODEL_PATH.'yp_form.class.php';
			$content_form = new content_form($modelid);
			$data = $yellowpage->get($id);
			$forminfos = $content_form->get($data);
		}
		include template('buy_edit.html','yellowpage');
		break;
		
	case 'delete':
		if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
	 
		$id = intval($id);
		$yellowpage->delete($id);
		showmessage('删除成功！', 'goback');
		include template('buy_manage.html','yellowpage');
		break;
		
		case 'show':
		cache_page_start();
		
		require_once 'includes/company.class.php';
		$company = new company();
		
		require_once 'includes/yellowpage.class.php';
		//$yellowpage = new yellowpage();
		$buyid = intval($id);
		//$yellowpage->set_model('buy');
		$yellowpage->hits($id);
		$rs = $yellowpage->get($id);
		$head['keywords'] .= $rs['title'].'_商机';
		$head['title'] .= $rs['title'].'_商机'.'_'.$SETTING['sitename'];
		$head['description'] .= $rs['title'].'_商机'.'_'.$SETTING['sitename'];
		if(!$rs || $rs['status'] != 99)showmessage('数据未通过审核或者已经被删除');
		$c = $company->get($rs['userid']);
		include template('buy_show.html','yellowpage');
		cache_page(intval($M['cache_list']));
	break;
	
	case 'search':
		$head['keywords'] .= '商机搜索';
		$head['description'] .= '商机搜索'.'_'.$SETTING['sitename'];
		$head['title'] .= '商机搜索'.'_'.$SETTING['sitename'];
		$CAT = subcat(0,'yellowpage'); 
		include template('buy_search.html','yellowpage');
		cache_page(intval($M['cache_list']));
	break;
	
	case 'searchlist':
		$head['keywords'] .= '商机搜索结果';
		$head['description'] .= '商机搜索'.'_'.$SETTING['sitename'];
		$head['title'] .= '商机搜索结果'.'_'.$SETTING['sitename'];
		$page = $page?$page:1;
		$catid = intval($catid);
		$q = addslashes(htmlspecialchars($q));
		$areaname = addslashes(htmlspecialchars($areaname));
		$lprice = intval($lprice);
		$hprice = intval($hprice);
		if($catid)
		{
			$c= new company();
			$r = $yellowpage->get_yp_arrchildid($catid);
			if($r['arrchildid'])$r['arrchildid'] = $catid.','.$r['arrchildid'];
			else $r['arrchildid'] = $catid;
			$where .= "b.catid IN ($catid) AND ";
		}
		if($lprice)$where .= "b.price >= '{$lprice}' AND ";
		if($hprice)$where .= "b.price <= '{$hprice}' AND ";
		if($areaname)
		{
			$areaname = htmlspecialchars($areaname);
			if(strpos(',',$areaname)===false)
			{
				$where .= " c.areaname='$areaname' AND ";
			}
			else
			{
				$where .= " c.areaname IN ($areaname) AND ";
			}
			$areaname = urlencode($areaname);
		}
		if($q)$where .= "b.title LIKE '%{$q}%' AND ";
		$where .= "c.userid = b.userid";
		include template('buy_searchlist.html','yellowpage');
		cache_page(intval($M['cache_list']));
	break;
	
	
	default:	
	
		$head['keywords'] .= '商机';
		$head['description'] .= '商机'.'_'.$SETTING['sitename'];
		$head['title'] .= '商机'.'_'.$SETTING['sitename'];
		if($catid)
		{
			if($child == 1) $arrchildid = subcat($catid,'yellowpage');
		}
		if($CATEGORY[$catid])
		{
			$where .= "b.catid IN (".$CATEGORY[$catid]['arrchildid'].")";
		}
		else
		{
			$where = 1;
		}
		$page = intval($page);
		if(!$page) $page = 1;
		if($areaname)
		{
			$areaname = addslashes(htmlspecialchars(urldecode($areaname)));
			$where .= " AND c.areaname = '{$areaname}'";
		}
		$pagesize = intval($pagesize);
		if(!$pagesize) $pagesize = 20;
		$viewtype = intval($viewtype);
		switch($view_type)
		{
			case 1:
			case 2:
			case 3:
			break;
			
			default:
			$view_type = 1;
			break;
		}
	
		switch($tid)
		{
			case '1':
			case '2':
			case '3':
			case '4':
			break;
			
			default:
			$tid = 0;
			break;
		}
		if($tid) $where .= " AND tid = '{$tid}'";
		if($order) $where .= " ORDER BY b.price ASC";
		else $where .= " ORDER BY b.price DESC"; 
		include template('buy.html','yellowpage');
		cache_page(intval($M['cache_list']));
	break;
	}
?>